﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShareB.Models;

namespace ShareB.Controllers
{
    public class PublicationController : Controller
    {
        //serviços necessários 
        PublicationService pub_serv = new PublicationService();
        PubConfService conf_serv = new PubConfService();
        IMembershipService user_serv = new UserMembershipService();

        // Acção que mapeia o acesso genérico á manutenção geral do sistema ( nao precisa de um modelo)
        // GET: /Configuration/

        public ActionResult Index()
        {
            return View();
        }

        // **************************************
        // Get
        // URL: /Publications/ChoiceConfiguration
        // **************************************

        public ActionResult ChoiceConfiguration()
        {
            IList<PubConfModel> model = new List<PubConfModel>();
            model = conf_serv.GetAllConfigurations();

            return View(model);
        }

        // **************************************
        // Get
        // URL: /Publications/ViewAllMyPub
        // **************************************

        public ActionResult ViewAllMyPub()
        {
            IList<PublicationModel> model = new List<PublicationModel>();
            model = pub_serv.GetAllMyPublication(user_serv.GetUserID(HttpContext.User.Identity.Name));

            return View(model);
        }

        // **************************************
        // GET
        // URL: /Publications/ViewMyPub
        // **************************************
        public ActionResult ViewMyPub(string id)
        {

            PublicationModel model = new PublicationModel();
            model = pub_serv.GetMyPublication(Convert.ToInt32(id));

            return View(model);
        }

        public ActionResult ViewMyPubTemp(PublicationModel model)
        {
            pub_serv.ActClassification(model);
            return RedirectToAction("ViewMyPub", new { id = model.PublicationID });
        }
        // **************************************
        // Get
        // URL: /Publications/CreatePublication
        // **************************************
        
        public ActionResult CreatePublication(string id)
        {
            NewPublicationModel model = new NewPublicationModel();
            model.configuration = conf_serv.GetConfigurations(Convert.ToInt32(id));
            foreach (MetaFieldModel m in model.configuration.fieldsConf)
            {
                    model.Fields.Add(new FieldModel
                    {
                        FieldName = m.FieldName,
                        MetaFieldID = m.MetaFieldID
                    });
            }
            model.PubConfID = Convert.ToInt32(id);
            model.UserID = user_serv.GetUserID(HttpContext.User.Identity.Name);
            model.Username = HttpContext.User.Identity.Name;
            model.StartDate = System.DateTime.Now;

            return View(model);
        }

        // **************************************
        // POST
        // URL: /Publications/CreatePublication
        // **************************************
        [HttpPost]
        public ActionResult CreatePublication(NewPublicationModel model)
        {
            if (ModelState.IsValid)
            {
                PublicationModel m = (PublicationModel)model;
                m.UserID = user_serv.GetUserID(HttpContext.User.Identity.Name);
                pub_serv.CreatePublication(m, m.PubConfID);
                return RedirectToAction("Index");
            }
            else
                ModelState.AddModelError("", "An error as ocurred");
                return View(model);

            
        }

        // **************************************
        // GET
        // URL: /Publications/EditPublication
        // **************************************
        public ActionResult EditPublication(string id)
        {
            PublicationModel model = new PublicationModel();
            model = pub_serv.GetMyPublication(Convert.ToInt32(id));
                       
            return View(model);
        }

        // **************************************
        // POST
        // URL: /Publications/EditPublication
        // **************************************
        [HttpPost]
        public ActionResult EditPublication(PublicationModel model) 
        {
            if (ModelState.IsValid)
            {
                model.UserID = user_serv.GetUserID(HttpContext.User.Identity.Name);
                pub_serv.EditPublication(model);

                return RedirectToAction("ViewMyPub", new { id = model.PublicationID });
            }
            else
                ModelState.AddModelError("", "An error as ocurred");
            return View(model);
        }


 
    }
}
